
from selenium import webdriver
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.common.by import By
import selenium.webdriver.support.expected_conditions as EC
import selenium.webdriver.support.ui as ui
from selenium.webdriver.common.keys import Keys
import requests
import os
import sys
import  uuid
from model.avinfo import AvInfo
CurrentPath = os.getcwd()
print(os.path)
print (sys.argv[0])
dirs=os.path.split( os.path.realpath( sys.argv[0] ))
print (dirs[0]+os.sep+"html"+os.sep+"img")
mainPage = webdriver.Chrome()
detailPage = webdriver.Chrome()
cook={'playno1':'playno1Cookie'}
detailPage.set_page_load_timeout(115)
detailPage.set_script_timeout(115)
detailPage.implicitly_wait(115)
detailPage.get('http://www.playno1.com')
detailPage.find_element_by_xpath(".//*[@class='ui-dialog-buttonset']/button[1]").click()
mainPage.set_page_load_timeout(115)
mainPage.set_script_timeout(115)
mainPage.implicitly_wait(115)
mainPage.get('http://www.playno1.com')
mainPage.find_element_by_xpath(".//*[@class='ui-dialog-buttonset']/button[1]").click()
mainPage.get('http://www.playno1.com/portal.php?mod=list&catid=78')

eles=mainPage.find_elements_by_xpath(".//*[@class='fire_float']/ul/li");
avinfos=[]
avinfo=AvInfo
htmldir=dirs[0]+os.sep+"html"
imgdir=dirs[0]+os.sep+"html"+os.sep+"img"
if os.path.exists(imgdir) == False:
    os.makedirs(imgdir)
for ele in eles:
    title=ele.find_element_by_tag_name("h3").text
    print(title)
    contenturl=ele.find_element_by_xpath(".//*[@class='fire_imgbox']/a").get_attribute("href")
    print(contenturl)
    image= ele.find_element_by_xpath(".//*[@class='fire_imgbox']/a/img").get_attribute("src")
    print(image)
    imagecontent=requests.get(image)
    with open(imgdir+os.sep+str(uuid.uuid1())+'.jpg', 'wb') as file:
        file.write(imagecontent.content)
    avinfo.title=title
    avinfo.imageurl=image
    avinfo.contenturl=contenturl
    avinfos.append(avinfo)

#mainPage.add_cookie(cook)